﻿Imports System.Resources
Imports System.IO
Imports System.Windows.Media.Imaging
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Class TypeNameToIconConverter
    Implements IValueConverter

    Private icoString As BitmapImage
    Private icoNum As BitmapImage
    Private icoDate As BitmapImage

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        If targetType IsNot GetType(ImageSource) Then _
            Throw New InvalidOperationException("La cible doit être de type System.Windows.Media.ImageSource")
        Try
            Select Case CType(value, String)
                Case "String"
                    If icoString Is Nothing Then
                        icoString = createBitmapImage(New Uri("C:\Pti\Travail\3005 - NATIXIS ASSURANCES\OMANE1.0\OMANE WPF\OMANE\OMANE.Common.Theme\Images\varCar16.ico", UriKind.Absolute))
                    End If
                    Return icoString
                Case "Double"
                    If icoNum Is Nothing Then
                        icoNum = createBitmapImage(New Uri("C:\Pti\Travail\3005 - NATIXIS ASSURANCES\OMANE1.0\OMANE WPF\OMANE\OMANE.Common.Theme\Images\varNum16.ico", UriKind.Absolute))
                    End If
                    Return icoNum
                Case "DateTime"
                    If icoDate Is Nothing Then
                        icoDate = createBitmapImage(New Uri("C:\Pti\Travail\3005 - NATIXIS ASSURANCES\OMANE1.0\OMANE WPF\OMANE\OMANE.Common.Theme\Images\varDate16.ico", UriKind.Absolute))
                    End If
                    Return icoDate
                Case Else
                    Return New BitmapImage
            End Select
        Catch ex As Exception
            Return DependencyProperty.UnsetValue
        End Try
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Function createBitmapImage(ByVal Uri As Uri) As BitmapImage
        Dim result As BitmapImage = New BitmapImage
        result.BeginInit()
        result.UriSource = Uri
        result.EndInit()
        Return result
    End Function

End Class
